package architect.springcloud.gateway.zuul.service.impl;

import architect.springcloud.gateway.zuul.domain.UserInfo;
import architect.springcloud.gateway.zuul.repository.UserInfoRepository;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import static java.util.Collections.emptyList;

/**
 * 用户Service
 *
 * @author guanxin
 * @date 2018/5/3 11:04
 */

@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    private UserInfoRepository userInfoRepository;

    public UserDetailsServiceImpl(UserInfoRepository userInfoRepository) {
        this.userInfoRepository = userInfoRepository;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserInfo userInfo = userInfoRepository.findByUsername(username);
        if (userInfo == null) {
            throw new UsernameNotFoundException(username);
        }
        return new User(userInfo.getUsername(), userInfo.getPassword(), emptyList());
    }
}
